package com.promis.crypto.symmetric;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class DESedeKeyCipher extends SymmetrickKeyCipher {
	
	public final static short LENGTH_112 = 112;
	public final static short LENGTH_168 = 168;

	private final static String KEY_ALGORITHM = "DESede";
	
	private final static String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";
	
	private static DESedeKeyCipher keyCipher;
	
	private DESedeKeyCipher() {
		super(KEY_ALGORITHM, CIPHER_ALGORITHM);
	}
	
	public synchronized static DESedeKeyCipher getInstance() {
		if (keyCipher == null) {
			keyCipher = new DESedeKeyCipher();
		}
		return keyCipher;
	}
	
	@Override
	public SecretKey toSecretKey(byte[] secretKey) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
		DESedeKeySpec des = new DESedeKeySpec(secretKey);  
	    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);  
	    return keyFactory.generateSecret(des);  
	}
}
